package com.ccp.dev.system.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.system.model.SysOrg;
import com.ccp.dev.system.model.UserPosition;
import com.ccp.dev.system.service.SysOrgService;
import com.ccp.dev.system.service.UserPositionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对象功能:SYS_USER_POS 控制器类
 * @author y
 */
@Controller
@Action(ownermodel = SysAuditModelType.POS_MANAGEMENT)
@RequestMapping(UrlConstants.PROJECT_ROOT+UrlConstants.PROJECT_MODULE_SYSTEM+"/userPosition")
public class UserPositionController extends SysBaseController {
	@Resource
	private UserPositionService userPositionService;

	@Resource
	private SysOrgService sysOrgService;

	/**
	 * 根据用户id或岗位id查询用户岗位关联
	 * @return ResultData
	 */
	@Action(description = "根据用户id或岗位id查询用户岗位关联",
			detail = "根据<#if StringUtil.isNotEmpty(userId)><#assign entity=sysUserService.getOneById(userId)/>用户【${entity.fullName}】" +
					"<#elseif StringUtil.isNotEmpty(posId)><#assign entity=positionService.getOneById(posId)/>岗位【${entity.posName}】" +
					"<#else>参数</#if>查询岗位<#if success>成功<#else>失败</#if>",
			exectype = "查询日志")
	@ResponseBody
	@GetMapping("queryByUserIdOrPosId")
	public ResultData queryByUserIdOrPosId(HttpServletRequest request){
//			String userId = RequestUtil.getString(request,"userId");
//			String posId = RequestUtil.getString(request,"posId");
//			SysAuditThreadLocalHolder.putParamerter("userId", userId);
//			SysAuditThreadLocalHolder.putParamerter("posId", posId);
//		try{
//			List list = this.userPositionService.queryByUserIdOrPosId(userId,posId);
//			SysAuditThreadLocalHolder.putParamerter("success", true);
//			return getResultDataSuccess("operation.success",0,list);
//		}catch (Exception e){
//			SysAuditThreadLocalHolder.putParamerter("success", false);
//			return getResultDataError("operation.failure");
//		}

		String userId = RequestUtil.getString(request,"userId");
		String selectOrgId = RequestUtil.getString(request,"selectOrgId");
		SysAuditThreadLocalHolder.putParamerter("userId", userId);
		try{
			List list=new ArrayList();
			if(StringUtil.isEmpty(userId)){
				Map org=new HashMap<>();
				SysOrg orgentity = sysOrgService.getSysOrgById(selectOrgId);
				if(orgentity!=null){
					org.put("orgId",orgentity.getOrgId());
					org.put("orgName",orgentity.getOrgName());
					org.put("isPrimary",1);
					org.put("isCharge",0);
					list.add(org);
				}
			}else{
				list = this.userPositionService.queryByUserIdOrPosId(userId,null);
			}
			SysAuditThreadLocalHolder.putParamerter("success", true);
			return getResultDataSuccess("operation.success",0,list);
		}catch (Exception e){
			SysAuditThreadLocalHolder.putParamerter("success", false);
			return getResultDataError("operation.failure");
		}
	}

	/**
	 *  根据组织id获取组织成员
	 * @param request
	 * @return
	 */
	@Action(description = "根据组织id获取组织成员",
			detail = "根据<#assign entity=sysOrgService.getSysOrgById(orgId)/>组织【${entity.orgName}】查询成员<#if success>成功<#else>失败</#if>",
			exectype = "查询日志")
	@ResponseBody
	@GetMapping("queryUserByOrgIds")
	public ResultData  queryUserByOrgIds(HttpServletRequest request){
		try {
			String orgId=RequestUtil.getString(request,"orgId");
			List<UserPosition> list = userPositionService.queryUserByOrgIds(orgId);
			SysAuditThreadLocalHolder.putParamerter("success", true);
			return getResultDataSuccess("operation.success",0,list);
		}
		catch (Exception e){
			SysAuditThreadLocalHolder.putParamerter("success", false);
			return getResultDataError("operation.failure");
		}
	}

}
